package it.antonio.ilpizze.swing.util;

import java.util.HashMap;
import java.util.Map;

public class Converters {
	
	
	public Converters() {
		super();
		init();
	}


	private Map<Class<?>, Converter<?>> converters = new HashMap<Class<?>, Converter<?>>();
	
	public <T> void add(Class<T> clazz, Converter<T> converter){
		converters.put(clazz, converter);
	}
	
	@SuppressWarnings("unchecked")
	public <T> Converter<T> get(Class<? extends T> clazz){
		return (Converter<T>) converters.get(clazz);
	}
	
	
	
	private void init() {
		
		add(String.class, new StringConverter());
		add(int.class, new IntegerConverter());
		add(Integer.class, new IntegerConverter());
		add(Boolean.class, new BooleanConverter());
		add(boolean.class, new BooleanConverter());
		
		add(char.class, new CharacterConverter());
		add(Character.class, new CharacterConverter());
		
		add(Long.class, new LongConverter());
		add(long.class, new LongConverter());
		
		add(Short.class, new ShortConverter());
		add(short.class, new ShortConverter());
		
		add(Double.class, new DoubleConverter());
		add(double.class, new DoubleConverter());
		
	}

	private class StringConverter implements Converter<String>{

		@Override
		public String toObject(String value) {
			return value;
		}

		@Override
		public String toString(String value) {
			return value;
		}
		
	}
	private class IntegerConverter implements Converter<Integer>{

		@Override
		public Integer toObject(String value) {
			int result;
			if (value == null || value == "") {
				result = 0;
			} else {
				result = new Integer(value);
			}
			return result;
		}

		@Override
		public String toString(Integer value) {
			return value.toString();
		}
		
	}
	
	private class BooleanConverter implements Converter<Boolean>{

		@Override
		public Boolean toObject(String value) {
			return Boolean.valueOf(value);
		}

		@Override
		public String toString(Boolean value) {
			return value.toString();
		}

		
	}
	
	private class CharacterConverter implements Converter<Character>{

		@Override
		public Character toObject(String value) {
			return value.charAt(0);
		}

		@Override
		public String toString(Character value) {
			return value.toString();
		}

		
		
	}
	
	private class LongConverter implements Converter<Long>{

		@Override
		public Long toObject(String value) {
			return new Long(value);
		}

		@Override
		public String toString(Long value) {
			return value.toString();
		}

		
		
	}
	
	private class ShortConverter implements Converter<Short>{

		@Override
		public Short toObject(String value) {
			return new Short(value);
		}

		@Override
		public String toString(Short value) {
			return value.toString();
		}

	}
	
	private class DoubleConverter implements Converter<Double>{

		@Override
		public Double toObject(String value) {
			return new Double(value);
		}

		@Override
		public String toString(Double value) {
			return value.toString();
		}

	}
	
}
